package com.zj.core.ui

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.constraint.ConstraintSet.HORIZONTAL
import android.support.constraint.ConstraintSet.PARENT_ID
import android.view.Gravity
import android.view.View
import android.view.ViewManager
import android.widget.Button
import android.widget.LinearLayout
import com.zj.core.R
import org.jetbrains.anko.*
import org.jetbrains.anko.constraint.layout.constraintLayout
import org.jetbrains.anko.constraint.layout.guideline
import org.jetbrains.anko.custom.ankoView

class PictureActivityUi(groupView:LinearLayout):AnkoComponent<Context>{
    private var view:LinearLayout=groupView
    lateinit var crop_image_view:com.edmodo.cropper.CropImageView
    lateinit var btn_rotate:Button
    lateinit var btn_ok:Button

    override fun createView(ui: AnkoContext<Context>): View= with(view){
        constraintLayout{
            lparams(width= matchParent,height= matchParent)

            crop_image_view=cropimageVview{
                id=R.id.crop_image_view
            }.lparams(width= matchParent,height = 0){
                bottomMargin=dip(15)
                topMargin=dip(15)
                topToTop=PARENT_ID
                bottomToTop=R.id.guide_line_bottom
            }

            guideline {
                id=R.id.guide_line_bottom
            }.lparams(width= wrapContent,height = wrapContent){
                orientation= ConstraintLayout.LayoutParams.HORIZONTAL
                guidePercent=0.85F
            }

            linearLayout {
                backgroundColor=context.resources.getColor(R.color.colorPrimary)
                gravity=Gravity.CENTER

                btn_rotate=button{
                    text="旋转"
                    id=R.id.btn_rotate
                }.lparams(width=0,height = wrapContent){
                    weight=1F
                }

                btn_ok=button{
                    text="裁剪"
                    id=R.id.btn_ok
                }.lparams(width=0,height = wrapContent){
                    weight=1F
                }
            }.lparams(width= matchParent,height = 0){
                orientation= HORIZONTAL
                bottomToBottom=PARENT_ID
                topToBottom=R.id.guide_line_bottom
            }
        }
    }

    fun ViewManager.cameracontainer(theme: Int = 0) = cropimageVview(theme) {}
    inline fun ViewManager.cropimageVview(theme: Int = 0, init: com.edmodo.cropper.CropImageView.() -> Unit) : com.edmodo.cropper.CropImageView {
        return ankoView({ com.edmodo.cropper.CropImageView(it) }, theme, init)
    }
}